package cn.tinlan.shopcoin.app.discover.adapter;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PaintDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import cn.tinlan.shopcoin.R;
import cn.tinlan.shopcoin.app.discover.model.DiscoverItemDataModel;

/**
 * Created by Mortimer on 2018/11/23. 19:19
 * 佛祖🙏，永无bug。
 */
public class ScrollLeftAdapter extends BaseQuickAdapter<DiscoverItemDataModel.ScrollItemBean, BaseViewHolder> {
    private List<TextView> tv = new ArrayList<>();
    PaintDrawable mDrawable;
    static final int REDS         = 0xFFF65F4D;
    public ScrollLeftAdapter(int layoutResId, @Nullable List<DiscoverItemDataModel.ScrollItemBean> data) {
        super(layoutResId, data);

    }

    @Override
    protected void convert(BaseViewHolder helper, DiscoverItemDataModel.ScrollItemBean item) {
        helper.setText(R.id.left_text, item.getName())
                .addOnClickListener(R.id.left_item);
        //将左侧item中的TextView添加到集合中
        tv.add((TextView) helper.getView(R.id.left_text));
        //设置进入页面之后,左边列表的初始状态
        if (tv != null && getData() != null && tv.size() == getData().size()) {
            selectItem(0);
        }
//        Log.i("sssssssssss","i"+item);
        helper.getView(R.id.left_item).setSelected(true);
    }

    //传入position,设置左侧列表相应item高亮
    public void selectItem(int position) {
        for (int i = 0; i < getData().size(); i++) {
//            Log.i("sssssssssss","i"+position);
            if (position == i) {
                if (mDrawable == null){
                    mDrawable= new PaintDrawable(REDS);
                    mDrawable.setCornerRadius(60);
                }

                tv.get(i).setBackground(mDrawable);
                tv.get(i).setTextColor(ContextCompat.getColor(mContext, R.color.white));

                //以下是指定某一个TextView滚动的效果
                tv.get(i).setEllipsize(TextUtils.TruncateAt.MARQUEE);
                tv.get(i).setFocusable(true);
                tv.get(i).setFocusableInTouchMode(true);
                tv.get(i).setMarqueeRepeatLimit(-1);
            } else {
                tv.get(i).setBackground(null);
                tv.get(i).setTextColor(ContextCompat.getColor(mContext, R.color.black));

                //失去焦点则停止滚动
                tv.get(i).setEllipsize(TextUtils.TruncateAt.END);
                tv.get(i).setFocusable(false);
                tv.get(i).setFocusableInTouchMode(false);
                tv.get(i).setMarqueeRepeatLimit(0);
            }
        }
    }
}
